package com.registtools.service.impl;

import com.registtools.dao.course.PushMessageDao;
import com.registtools.entity.PushMessageEntity;
import com.registtools.util.DateUtils;
import com.registtools.util.JsonUtil;
import com.registtools.util.YachNoticeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PushMessageImpl {
    //群机器人：Webhook
    @Value("${yach.url}")
    String URL;
    //群机器人：秘钥
    @Value("${yach.sign}")
    String SIGN ;
    @Autowired
    PushMessageDao pushMessageDao;

    /**
     * 统计后的结果发送至知音楼机器人消息
     *
     * @return*/
    @Scheduled(cron = "0 0/1 * * * ?")
    public String pushMsg(){
         //获取发送内推=>发送内容为:map格式
        Map<String, Object> getMap = getCountClasses();
        //调用知音楼机器人发送消息
        YachNoticeUtil.sendMsgText(JsonUtil.mapToJson(getMap),URL,SIGN);
        return "repStr";
    }
    /**
     * 统计开课城市数据分组实现
     *
     * @return*/
    public Map<String, Object> getCountClasses(){
        Map<String,Object> setMap = new HashMap<>();
        Map<String,Object> countMap = new HashMap<>();
        countMap.put("startTime",DateUtils.getByBeforeDay(6));
        countMap.put("endTime",DateUtils.getSystime());
       List<PushMessageEntity> list = pushMessageDao.getCountClasses(countMap);
       list.forEach(pm ->{
           if (pm.getClassesNumber() >= 100) {
               if (pm.getCity_id().equals("010")) {
                   setMap.put("北京校区今日开课数量： ", pm.getClassesNumber());
               }
               if (pm.getCity_id().equals("020")) {
                   setMap.put("上海校区今日开课数量： ", pm.getClassesNumber());
               }
               if (pm.getCity_id().equals("1111")) {
                   setMap.put("测试校区今日开课数量： ", pm.getClassesNumber());
               }
           }
       } );
        return setMap;
    }

}
